<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once APP_ROOT_DIR . 'modules/knowledgebase/entry/inc/KBEntryModel.php';
require_once APP_ROOT_DIR . 'modules/knowledgebase/comment/inc/KBCommentModel.php';
require_once APP_ROOT_DIR . 'modules/knowledgebase/category/inc/KBCategoryModel.php';
require_once APP_ROOT_DIR . 'modules/knowledgebase/comment/inc/KBCommentView_helper.php';


class KBHomeView_default extends AppView
{
	
	var $num = 10;
	
	function execute() {
		
		$tpl = &new tplTemplatez($this->template_dir . 'page.html');
		
		//$tpl->tplAssign('most_viewed', $this->getMostViewedQuestions());
		$tpl->tplAssign('most_viewed', $this->getRecentArticles());
		$tpl->tplAssign('recently_posted', $this->getRecentlyPostedComments());
		//$tpl->tplAssign('quick_link', $this->getQuickLinks());		
		
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getMostViewedQuestions() {
		
		$entry = &new KBEntryModel;
		$entry->setSqlParamsOrder('ORDER BY hits DESC');
		
		$this->priv->setOwnParam($entry->getOwnParams($this->priv));
		$entry->setSqlParams('AND ' . $this->priv->getOwnParam('kb_entry'));
		$rows = &$this->stripVars($entry->getRecords(10, 0));
		
		$tpl = &new tplTemplatez($this->template_dir . 'most_viewed.html');
		
		foreach($rows as $k => $v) {
			
			$more = array('id'=>$v['id']);
			$link =  $this->controller->getLink('knowledgebase', 'kb_entry', false, 'update', $more);
			$v['entry_link'] = $link;
		
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getRecentArticles() {
		
		$entry = &new KBEntryModel;
		$entry->setSqlParamsOrder('ORDER BY date_posted DESC');
		
		$this->priv->setOwnParam($entry->getOwnParams($this->priv));
		$entry->setSqlParams('AND ' . $this->priv->getOwnParam('kb_entry'));
		$rows = &$this->stripVars($entry->getRecords(10, 0));
		
		$tpl = &new tplTemplatez($this->template_dir . 'recent_article.html');
		
		foreach($rows as $k => $v) {
			
			$more = array('id'=>$v['id']);
			$link =  $this->controller->getLink('knowledgebase', 'kb_entry', false, 'update', $more);
			$v['entry_link'] = $link;
			$v['status_num'] = $v['active'] ? '1' : '0';
			$v['date'] = $this->getFormatedDate($v['ts']);
		
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getRecentlyPostedComments() {
		
		$entry = &new KBCommentModel;
		$entry->setSqlParamsOrder('ORDER BY date_posted DESC');
		
		$this->priv->setOwnParam(sprintf('e.author_id=%d', $this->priv->user_id));
		$entry->setSqlParams('AND ' . $this->priv->getOwnParam('kb_entry'));
		$rows = &$this->stripVars($entry->getRecords(10, 0));
		
		//BB CODE
		$parser = &KBCommentView_helper::getBBCodeObj();
		
		$tpl = &new tplTemplatez($this->template_dir . 'recent_comment.html');
		
		foreach($rows as $k => $v) {
		
			$more = array('id'=>$v['id']);
			$link =  $this->controller->getLink('knowledgebase', 'kb_comment', false, 'update', $more);
			
			$v['entry_link'] = $link;
			$v['status_num'] = $v['active'] ? '1' : '0';
			$v['comment'] =  $parser->qparse($v['comment']);
			$v['date'] = $this->getFormatedDate($v['ts']);
			
			$tpl->tplParse($v, 'row');
		}
		
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
	
	
	function getQuickLinks() {
	
		$this->addMsg('file_msg.ini');
	
		$tpl = &new tplTemplatez($this->template_dir . 'quick_link.html');
		
		$module = 'knowledgebase';
		$tpl->tplAssign('entry_link', $this->controller->getLink($module, 'kb_entry', false, 'insert'));
		$tpl->tplAssign('category_link', $this->controller->getLink($module, 'kb_category', false, 'insert'));	
		$tpl->tplAssign('glossary_link', $this->controller->getLink($module, 'kb_glossary', false, 'insert'));			
		
		$module = 'file';
		$tpl->tplAssign('file_entry_link', $this->controller->getLink($module, 'file_entry', false, 'insert'));
		$tpl->tplAssign('file_category_link', $this->controller->getLink($module, 'file_category', false, 'insert'));			
		
		
		if($this->priv->isPriv('insert', 'kb_entry')) {
			$tpl->tplSetNeeded('/add_entry');
		}
		
		if($this->priv->isPriv('insert', 'kb_category')) {
			$tpl->tplSetNeeded('/add_entry_category');
		}
		
		if($this->priv->isPriv('insert', 'kb_glossary')) {
			$tpl->tplSetNeeded('/add_glossary');
		}		
		
		if($this->priv->isPriv('insert', 'file_entry')) {
			$tpl->tplSetNeeded('/add_file');
		}		
		
		if($this->priv->isPriv('insert', 'file_category')) {
			$tpl->tplSetNeeded('/add_file_category');
		}		
		
		$tpl->tplAssign($this->msg);
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}
}

/*
Total Questions: 69
Total Visible: 63 
Total Hidden: 6
Total Categories: 72
Total Root: 38 
Total Sub: 34
Total Glossary Words: 34 
Total Comments: 162
Total Approved: 150 
Total Disapproved: 12 
Total Pending: 0
*/

// Recently Posted Comments
?>
